package top.xiaocao.jpa.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import top.xiaocao.jpa.model.User;
import top.xiaocao.jpa.repository.UserRepository;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserRepository userRepository;

	@Override
	public void add(User user) {
		userRepository.save(user);
	}
	
	@Override
	public void addWithoutTransaction(User user) {
		boolean result = TransactionSynchronizationManager.isActualTransactionActive();
		System.out.println("is in transaction " + result);
		userRepository.save(user);
	}

	@Override
	public void addAndThrowException(User user) {
		userRepository.save(user);
		int i = 1;
		if (i == 1) {
			throw new RuntimeException("test exception");
		}
	}

	

}
